﻿using Digitalmes.Application.Sys.Dicts;
using Digitalmes.Application.Sys.Dicts.Commands;
using Digitalmes.Application.Sys.Dicts.Queries;

namespace Digitalmes.WebApi.Endpoints.Sys;

/// <summary>
/// 字典数据终结点
/// </summary>
public sealed class DictEndpoint : EndpointGroupBase
{
    public override void Map(WebApplication app)
    {
        app.MapGroup(this)
           .RequireAuthorization()
           .MapGet(GetDictPagedListAsync, "list-page")
           .MapPost(CreateDictAsync)
           .MapPut(UpdateDictAsync)
           .MapDelete(DeleteDictAsync, "{id}")
           .MapGet(GetDictDataPagedListAsync, "data/list-page")
           .MapPost(CreateDictDataAsync, "data")
           .MapPut(UpdateDictDataAsync, "data")
           .MapDelete(DeleteDictDataAsync, "data/{id}");
    }

    public async Task<IApiResult> GetDictPagedListAsync(ISender sender, [AsParameters] GetDictWithPagedQuery query)
    {
        var result = await sender.Send(query);
        return ApiResult.FromResult(result);
    }

    public async Task<IApiResult> CreateDictAsync(ISender sender, SysDictDto dto)
    {
        var result = await sender.Send(new CreateDictCommand(dto));
        return ApiResult.FromResult(result);
    }

    public async Task<IApiResult> UpdateDictAsync(ISender sender, SysDictDto dto)
    {
        var result = await sender.Send(new UpdateDictCommand(dto));
        return ApiResult.FromResult(result);
    }

    public async Task<IApiResult> DeleteDictAsync(ISender sender, long id)
    {
        var result = await sender.Send(new DeleteDictCommand(id));
        return ApiResult.FromResult(result);
    }

    public async Task<IApiResult> GetDictDataPagedListAsync(ISender sender, [AsParameters] GetDictDataWithPagedQuery query)
    {
        var result = await sender.Send(query);
        return ApiResult.FromResult(result);
    }

    public async Task<IApiResult> CreateDictDataAsync(ISender sender, SysDictDataDto dto)
    {
        var result = await sender.Send(new CreateDictDataCommand(dto));
        return ApiResult.FromResult(result);
    }

    public async Task<IApiResult> UpdateDictDataAsync(ISender sender, SysDictDataDto dto)
    {
        var result = await sender.Send(new UpdateDictDataCommand(dto));
        return ApiResult.FromResult(result);
    }

    public async Task<IApiResult> DeleteDictDataAsync(ISender sender, long id)
    {
        var result = await sender.Send(new DeleteDictDataCommand(id));
        return ApiResult.FromResult(result);
    }
}
